package com.bailian.snails.config;

import com.bailian.snails.filter.Customer2GatewayFilter;
import com.bailian.snails.filter.MyGatewayFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author gaoxu
 * date: Created on 2021/7/21 下午3:04
 */
@Configuration
public class RoutesConfig {

    @Bean
    public MyGatewayFilter myGatewayFilter() {
        return new MyGatewayFilter();
    }

    @Bean
    public Customer2GatewayFilter customerGatewayFilter() {
        return new Customer2GatewayFilter();
    }

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder) {
        return routeLocatorBuilder.routes().route(r -> r.path("/ribbon/**")
                .filters(f -> f.stripPrefix(1)
                        .filter(myGatewayFilter())
                        .addRequestHeader("X-Response-Default-Foo", "Default-Bar"))
                .uri("lb://EUREKA-RIBBON")
                .order(0)
                .id("ribbon-route")
        ).build();
    }

    @Bean
    public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/mp-supplier-paas/heartbeat/ping3")
                        .filters(f -> f.filter(customerGatewayFilter())
                                .addResponseHeader("X-Response-test", "test")
                                .addRequestHeader("X-Request-test", "test"))
                        .uri("http://127.0.0.1:7210")
                        .id("mp_paas_supplier")
                ).build();
    }
}
